package com.xd.clap.services.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xd.clap.entity.GridOperator;
import com.xd.clap.entity.request.PageNumVO;
import com.xd.clap.entity.response.Result;
import com.xd.clap.mapper.GridOperatorMapper;
import com.xd.clap.services.GridOperatorService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class GridOperatorServiceImpl extends ServiceImpl<GridOperatorMapper, GridOperator> implements GridOperatorService {


    @Resource
    private GridOperatorMapper gridOperatorMapper;

    @Override
    public Result getGrid(GridOperator gridOperator,PageNumVO pageNumVO){
        PageHelper.startPage(pageNumVO.getCurrent(),pageNumVO.getSize());
        QueryWrapper<GridOperator> wrapper = new QueryWrapper<GridOperator>();
        if (StringUtils.isNotBlank(gridOperator.getGridName())) {
            wrapper.like("grid_name", gridOperator.getGridName());
        }
        if (StringUtils.isNotBlank(gridOperator.getPhonenum())) {
            wrapper.like("phonenum", gridOperator.getPhonenum());
        }
        if (StringUtils.isNotBlank(gridOperator.getBelongs())) {
            wrapper.like("belongs", gridOperator.getBelongs());
        }
        List<GridOperator> gridOperators = gridOperatorMapper.selectList(gridOperator==null? null : wrapper);
        PageInfo<GridOperator> gridOperatorPageInfo = new PageInfo<>(gridOperators);
        return Result.success(gridOperatorPageInfo, "查询成功！");
    }

    @Override
    public Result updateGrid(GridOperator gridOperator){
        GridOperator query = gridOperatorMapper.selectById(gridOperator.getGridId());
        GridOperator update = new GridOperator();
        update.setGridId(gridOperator.getGridId());
        update.setGridName(gridOperator.getGridName());
        update.setBelongs(gridOperator.getBelongs());
        update.setGridDuties(gridOperator.getGridDuties());
        if(!query.getPhonenum().equals(gridOperator.getPhonenum())){
            QueryWrapper<GridOperator> wrapper = new QueryWrapper<GridOperator>()
                    .select("grid_id","grid_name")
                    .eq("phonenum", gridOperator.getPhonenum());
            GridOperator info = gridOperatorMapper.selectOne(wrapper);
            if(info!= null){
                return Result.fail(0, "该手机号已存在");
            }
            update.setPhonenum(gridOperator.getPhonenum());
        }
        int updateGrid = gridOperatorMapper.updateById(update);
        if (updateGrid < 1) {
            return Result.fail(0, "更新失败");
        }

        return Result.success(updateGrid, "更新成功！");
    }

    @Override
    public Result addGrid(GridOperator gridOperator){
        QueryWrapper<GridOperator> wrapper = new QueryWrapper<GridOperator>()
                .select("grid_id","grid_name")
                .eq("phonenum", gridOperator.getPhonenum());
        GridOperator query = gridOperatorMapper.selectOne(wrapper);
        if(query!= null){
            return Result.fail(0, "该负责人已存在");
        }
        GridOperator add = new GridOperator()
                .setGridName(gridOperator.getGridName())
                .setBelongs(gridOperator.getBelongs())
                .setPhonenum(gridOperator.getPhonenum())
                .setGridDuties(gridOperator.getGridDuties());
        int addGrid = gridOperatorMapper.insert(add);
        if (addGrid < 1) {
            return Result.fail(0, "添加失败");
        }

        return Result.success(addGrid, "添加成功！");
    }

    @Override
    public Result deleteGrid(Integer gridId){
        int deleteGrid = gridOperatorMapper.deleteById(gridId);
        if (deleteGrid < 1) {
            return Result.fail(0, "删除失败");
        }

        return Result.success(deleteGrid, "删除成功！");
    }

    @Override
    public Result getGridAll(){
        List<GridOperator> gridOperators = gridOperatorMapper.selectList(null);
        return Result.success(gridOperators, "查询成功！");
    }
}
